package com.erp.module.ProductionExecute

import android.content.Context
import android.util.Log
import com.alibaba.sdk.android.push.common.util.JSONUtils
import com.erp.Interface.OnUserDataInterface
import com.erp.bean.ProductionExecuteBean
import com.erp.module.ProductionExecuter.FragmengtProductionExecute
import com.erp.pull.BasePullP
import com.erp.util.JsonUtil
import com.erp.util.Myutil
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject
import java.util.ArrayList

class ProductionExecuteP(var V : ProdutionExecuteContract.ProductionFragmentView, var context: Context,var sousuoBean: FragmengtProductionExecute.SousuoBean, var mlist : MutableList<ProductionExecuteBean>): BasePullP(),ProdutionExecuteContract.ProductionFragmentKP {
    var md = ProductionExecuteM()
    var code : Int = 0
    override fun getData(page: Int, pagesize: Int, onUserDataInterface: OnUserDataInterface) {
        md.getProductionList(context,page,pagesize,sousuoBean,onUserDataInterface)
    }
    init {
        PullV=V
    }
    override fun jsondata(data: String?) {
        Log.d("onSuccess",data)
        if(data==null){
            return
        }
        try {
            var jsd = JSONObject(data)
            var jsm = JsonUtil.getjovolue(jsd,"msg")
            var js = JsonUtil.getjsvolue(jsm,"instruct")

            var plmd_id : String = "";
            for (i in 0 until js.length()){
                var jo: JSONObject = js.get(i) as JSONObject
                if(plmd_id != JsonUtil.getvolue(jo,"id")){
                    var bean :ProductionExecuteBean = ProductionExecuteBean()
                    bean.plmd_id = JsonUtil.getvolue(jo,"id");
                    bean.count = JsonUtil.getvolue(jo,"sum")
                    bean.elevator_number = JsonUtil.getvolue(jo,"elevator_number")
                    bean.heap_number = JsonUtil.getvolue(jo,"heap_number")
                    bean.productive_time = JsonUtil.getvolue(jo,"productive_time")
                    bean.thickness = JsonUtil.getvolue(jo,"thickness")
                    bean.stock = JsonUtil.getvolue(jo,"stock")

                    if(bean.stock == "0"){
                        bean.stock_name = "未备料"
                    }else if(bean.stock == "1"){
                        bean.stock_name = "已备料"
                    }
                    var status = JsonUtil.getvolue(jo,"status")
                    if(status == "2"){
                        bean.status_name = "未送料"
                    }else if(status == "3"){
                        bean.status_name = "请求送料"
                    }else if(status == "4"){
                        bean.status_name = "正在送料"
                    }
                    bean.status = status
                    bean.workshop = JsonUtil.getvolue(jo,"workshop")
                    mlist.add(bean)
                    var yhhbean = ProductionExecuteBean.plmd_yhhbean()
                    yhhbean.yhh_id = JsonUtil.getvolue(jo,"yhh_id")
                    yhhbean.amount = JsonUtil.getvolue(jo,"amount")
                    yhhbean.thickness = JsonUtil.getvolue(jo,"thickness")
                    yhhbean.productive_time = JsonUtil.getvolue(jo,"productive_time")
                    yhhbean.color = JsonUtil.getvolue(jo,"color")
                    yhhbean.iu_name = JsonUtil.getvolue(jo,"iu_name")
                    yhhbean.iu_name2 = JsonUtil.getvolue(jo,"iu_name2")
                    mlist[mlist.size-1].listyyh.add(yhhbean)
                }else if(plmd_id == JsonUtil.getvolue(jo,"id")){
                    var yhhbean = ProductionExecuteBean.plmd_yhhbean()
                    yhhbean.yhh_id = JsonUtil.getvolue(jo,"yhh_id")
                    yhhbean.amount = JsonUtil.getvolue(jo,"amount")
                    yhhbean.thickness = JsonUtil.getvolue(jo,"thickness")
                    yhhbean.productive_time = JsonUtil.getvolue(jo,"productive_time")
                    yhhbean.color = JsonUtil.getvolue(jo,"color")
                    yhhbean.iu_name = JsonUtil.getvolue(jo,"iu_name")
                    yhhbean.iu_name2 = JsonUtil.getvolue(jo,"iu_name2")
                    mlist[mlist.size-1].listyyh.add(yhhbean)
                }
                plmd_id = JsonUtil.getvolue(jo,"id")
            }
        }catch (e : JSONException){
            Log.d("onSuccess",e.toString())
            e.printStackTrace()
            return
        }
        Log.d("projsondata","code="+code)
        if(code == 1){
            V.beiliao()
        }
        Log.d("projsondata",mlist.toString())
    }

    override fun listClean() {
        mlist.clear()
    }

    override fun getcategorys() {
        var zhiliangid: MutableList<String> = ArrayList()
        var zhiliangname: MutableList<String> = ArrayList()
        md.getcategorys(context,object : OnUserDataInterface{
            override fun onNot(throwable: String?) {
                Log.d("throwable","throwable="+throwable.toString())
                if (throwable != null) {
                    V.getcategoryNo(throwable)
                }
            }

            override fun onUserData(data: String?) {
                val jo = JSONObject(data)
                val keys = jo.keys()
                while (keys.hasNext()) {
                    val key = keys.next()
                    zhiliangid.add(key)
                    zhiliangname.add(JsonUtil.getvolue(jo, key))
                }
                V.getcategoryOk(zhiliangname,zhiliangid)
            }

        })
    }

    override fun setcode(c:Int) {
        Log.d("projsondata","code="+c)
        code = c
    }
}